In [1]:
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
import folium
from folium.plugins import HeatMap
In [2]:
# read in layer and point data
data_df = pd.read_csv('mps_robbery_policeuk12m.csv')
bocu_gdf = gpd.read_file('bocu_wgs.geojson')
In [3]:
# create point geo data
geometry = [Point(xy) for xy in zip(data_df['longitude'], data_df['latitude'])]
data_gdf = gpd.GeoDataFrame(data_df, geometry=geometry)
data_gdf = data_gdf.set_crs(4326)
In [4]:
# check coordinate reference system
print('CRS for data_gdf:', data_gdf.crs)
print('CRS for bocu_gdf:', bocu_gdf.crs)
CRS for data_gdf: epsg:4326 CRS for bocu_gdf: epsg:4326
In [5]:
valid_geom = data_gdf[data_gdf.geometry.is_valid]
print(valid_geom.geom_type.unique())
valid_geom = bocu_gdf[bocu_gdf.geometry.is_valid]
print(valid_geom.geom_type.unique())
['Point'] ['MultiPolygon']
In [6]:
may2022 = data_gdf.query("month == '2022 May'").copy()
jun2022 = data_gdf.query("month == '2022 Jun'").copy()
Create an interactive heatmap with polygon boundaries¶
In [7]:
# map points as a heatmap on folium
# create map
m = folium.Map(location=[51.5074, -0.1278], zoom_start=14)
# Add heatmap layer
heat_data = [[point.y, point.x] for point in data_gdf.geometry]
HeatMap(
heat_data,
radius=15, # radius of each data points influence in pixels
blur=10, # radius of blur in heatmap pixels
gradient={0.3: 'white', 0.6: 'yellow', 0.8: 'orange', 0.9: 'red', 1: 'magenta'}, # setting colour gradient
overlay=True,
name='Robbery Hotspots').add_to(m)
# Add boundary layer
folium.GeoJson(
bocu_gdf,
name='BOCU',
style_function=lambda x: {
'color': 'black',
'weight': 2,
'fillOpacity': 0
}
).add_to(m)
folium.LayerControl().add_to(m)
m
Out[7]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Create multiple interactive heatmap with polygon boundaries¶
In [8]:
# map points as a heatmap on folium
# create map
m = folium.Map(location=[51.5074, -0.1278], zoom_start=14)
# Add heatmap layers
heat_data1 = [[point.y, point.x] for point in may2022.geometry]
heat_data2 = [[point.y, point.x] for point in jun2022.geometry]
HeatMap(
heat_data1,
radius=15, # radius of each data points influence in pixels
blur=10, # radius of blur in heatmap pixels
gradient={0.3: 'white', 0.6: 'yellow', 0.8: 'orange', 0.9: 'red', 1: 'magenta'}, # setting colour gradient
overlay=True,
name='May Hotspots').add_to(m)
HeatMap(
heat_data2,
radius=15, # radius of each data points influence in pixels
blur=10, # radius of blur in heatmap pixels
gradient={0.3: 'white', 0.6: 'yellow', 0.8: 'orange', 0.9: 'red', 1: 'magenta'}, # setting colour gradient
overlay=True,
name='June Hotspots').add_to(m)
# Add boundary layer
folium.GeoJson(
bocu_gdf,
name='BOCU',
style_function=lambda x: {
'color': 'black',
'weight': 2,
'fillOpacity': 0
}
).add_to(m)
folium.LayerControl().add_to(m)
m
Out[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add a point layer¶
In [9]:
# map points as a heatmap on folium
# create map
m = folium.Map(location=[51.5074, -0.1278], zoom_start=12)
# Add heatmap layer
heat_data1 = [[point.y, point.x] for point in may2022.geometry]
heat_data2 = [[point.y, point.x] for point in jun2022.geometry]
HeatMap(
heat_data1,
radius=15, # radius of each data points influence in pixels
blur=10, # radius of blur in heatmap pixels
gradient={0.3: 'white', 0.6: 'yellow', 0.8: 'orange', 0.9: 'red', 1: 'magenta'}, # setting colour gradient
overlay=True,
name='May Hotspots').add_to(m)
HeatMap(
heat_data2,
radius=15, # radius of each data points influence in pixels
blur=10, # radius of blur in heatmap pixels
gradient={0.3: 'white', 0.6: 'yellow', 0.8: 'orange', 0.9: 'red', 1: 'magenta'}, # setting colour gradient
overlay=True,
name='June Hotspots').add_to(m)
# Add point layer
point_layer = folium.FeatureGroup(name='Point data')
for index, row in may2022.iterrows():
latitude=row['latitude']
longitude=row['longitude']
point = folium.Circle(
location=[latitude, longitude],
radius=2,
color='black',
fill=True
)
point.add_to(point_layer)
point_layer.add_to(m)
# Add boundary layer
folium.GeoJson(
bocu_gdf,
name='BOCU',
style_function=lambda x: {
'color': 'black',
'weight': 2,
'fillOpacity': 0
}
).add_to(m)
folium.LayerControl().add_to(m)
m
Out[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook